package com.example.weixinsystem.mq;

import com.alibaba.fastjson2.JSONObject;
import com.example.mq.expend.ExpendReceiver;
import com.example.weixinsystem.service.SysLogService;
import com.rabbitmq.client.Channel;
import com.weixin.common.core.constant.RabbitConstants;
import com.weixin.common.core.entity.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @version 1.0.0
 * @className: LogController
 * @description: 使用Mq消费日志，保存日志记录
 * @author: zhangjunfa
 * @date: 2023/7/5 18:03
 */
@Slf4j
@Component
public class SysLogExpend {
    private SysLogService sysLogService;

    public SysLogExpend(SysLogService sysLogService) {
        this.sysLogService = sysLogService;
    }

    @RabbitListener(queues = RabbitConstants.QUEUE_NAME)
    public void process(String msg, Channel channel, Message message) {
        log.info("队列：{},消费者：{}，通道编号：{}，发送的系统日志消息：{}", RabbitConstants.QUEUE_NAME, msg, channel.getChannelNumber(), new String(message.getBody()));
        SysLog sysLog = JSONObject.parseObject(msg, SysLog.class);
        sysLogService.addSysLog(sysLog);
    }
}
